<template>
    <el-dialog v-model="showDialog" :title="props.type=='add'?'添加字典类':'修改字典类'" @close="close" style="width: 30%">
            <el-form ref="dataForm" :rules="rules" :model="form" label-width="100px" align="center">
                <el-form-item label="字典名" prop="name">
                    <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="字典编码" prop="code">
                    <el-input v-model="form.code" :disabled="props.type=='edit'"/>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" />
                </el-form-item>
                <el-form-item>
                    <el-button v-show='props.type=="add"' type="primary" plain  @click="addDicType">添加</el-button>
                    <el-button v-show="props.type=='edit'" type="primary" plain  @click="updateDicType">更新</el-button>
                    <el-button @click="close" type="primary" plain >取消</el-button>
                </el-form-item>
            </el-form>
    </el-dialog>
</template>

<script setup>
    import {ref,reactive,onBeforeUpdate,onUpdated} from 'vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    let dataForm = ref(null)
    const  emits = defineEmits(['close','reload'])
    let props = defineProps(['open','type','id'])
    let showDialog = ref(false)
    let form = reactive({})
    const rules = reactive({
        name: [{ required: true, message: '请输入字典名', trigger: 'blur' },],
        code: [{required: true,message: '请输入字典编码', trigger: 'blur'},],
        remark: [{required: true,message: '请输入备注', trigger: 'blur'},],
    })
    onBeforeUpdate(function () {

    if(showDialog.value == true){
        if(props.type=='edit'){
            request.get(`/api/dictionary/getDicType/${props.id}`).then(resp => {
                for(let key in resp){
                     form[key] = resp[key]
                }
            })
        }else if(props.type=="add"){
            for(let key in form){
                delete form[key]
            }
         }
      }
    })

    function close(){
        showDialog.value = false
        dataForm.value.resetFields()//重置表单校验
    }

    function addDicType(){
        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/dictionary/add", form).then(resp => {
                    close()
                    emits("reload")
                    ElMessage({message: '添加成功！', type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }
    function updateDicType(){
        dataForm.value.validate((valid, fields)=> {
            if (valid) {
                request.post("/api/dictionary/update",form).then(resp => {
                    close()
                    emits("reload")
                    ElMessage({message: '修改成功！',type: 'success'})
                })
            }else{
                ElMessage({message: '缺少必填项！',type: 'error'})
            }
        })
    }

    defineExpose({showDialog})
</script>

<style scoped>

</style>